//
//  HTTPClientConfig.m
//  HTTPClient
//
//  Created by inphase on 2017/10/11.
//  Copyright © 2017年 inphase. All rights reserved.
//

#import "HTTPClientConfig.h"

@interface HTTPClientConfig ()


@property (nonatomic,copy) NSString *baseConfigFileName;
@property (nonatomic,copy) NSString *urlsFileName;


@end



@implementation HTTPClientConfig

- (instancetype)init
{
    self = [super init];
    if (self) {
        _baseConfigFileName = @"httpclient_config.plist";
        _urlsFileName = @"httpclient_urls.plist";
    }
    return self;
}

+(HTTPClientConfig *)sharedHTTPClientConfig {
    static HTTPClientConfig *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HTTPClientConfig alloc]init];
    });
    
    return instance;
}

-(NSDictionary *) configInfo {
    return [self infoWithFileName:_baseConfigFileName];
}

-(NSDictionary *) urlsInfo {
    return [self infoWithFileName:_urlsFileName];
}

-(NSDictionary *) infoWithFileName:(NSString *) fileName {
    
    if (![fileName.pathExtension isEqualToString:@"plist"]) {
        fileName = [fileName stringByAppendingPathExtension:@"plist"];
    }
    
    // 检测文件是否存在
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@""];
    NSAssert(path, ([NSString stringWithFormat:@"文件:%@不存在",fileName]));
    return [[NSDictionary alloc]initWithContentsOfFile:path];
}

-(void)setURLSFileName:(NSString *)name {
    _urlsFileName = name;
}

-(void)setBaseConfigFileName:(NSString *)name {
    _baseConfigFileName = name;
}

-(NSTimeInterval)timeoutInterval {
    return [[self configInfo][@"timeoutInterval"] doubleValue];
}

-(NSString *)debugURL {
    return [self configInfo][@"debug_url"];
}

-(NSString *)releaseURL {
    return [self configInfo][@"release_url"];
}

-(BOOL)isDebug {
    return [[self configInfo][@"debug"] boolValue];
}

-(NSString *)hostURL {
    if ([self isDebug]) {
        return [self debugURL];
    }
    return [self releaseURL];
}

-(NSString *)urlWithKey:(NSString *)key {
    return [[self urlsInfo] objectForKey:key];
}

@end
